const query = {
  page: '1',
  limit: '3'
}
const artinfo = {

}
const myArticle = async () => {

  const { data: { data: { count, totalPages, pageSize, currentPage, data } } } = await axios.get('/user/myArticle', { params: query })
  // const { count, totalPages, pageSize, currentPage, } = data
  // if (!count) {
  //   return
  // }
  const htmlStr = data.map(ele => {
    const { id, click, title, status, tags, lovenum, pic, cateid, author, content } = ele
    let picSrc
    if (pic) {
      picSrc = `http://124.223.14.236:8060/${pic}`
    }
    else {
      picSrc = `./static/images/default.png`
    }
    if (status === 1) {
      artinfo[id] = {
        title,
        author,
        cateid,
        tags,
        pic,
        content,
        status: '2',
        id
      }
      return `<li>
                    <a  href="javascript:;">
                      <span class="thumbnail">
                        <img class="thumb" src="${picSrc}" />
                      </span>
                      <span class="text">${title} 【待发布】</span>
                      <div class="action-btn">
                        <button  class="btn btn-primary btn-xs" data-id="${id}"><i class="fa fa-edit"></i>&nbsp;修改</button>
                         <button  class="btn btn-success btn-xs" data-id="${id}"><i class="fa fa-send"></i>&nbsp;发布</button>
                          <button  class="btn btn-danger btn-xs" data-id="${id}"><i class="fa fa-trash"></i>&nbsp;删除</button>
                      </div>
                    </a>
                  </li>`
    }
    else {
      return `
                  <li>
                    <a  href="javascript:;">
                      <span class="thumbnail">
                        <img class="thumb" src="${picSrc}" />
                      </span>
                      <span class="text">${title} 【已发布】</span>
                       <span class="text-muted post-views"> <i class="fa fa-eye"></i> 已阅读(${click||0})</span>
                       <span class="text-muted post-views"> <i class="fa fa-heart"></i> 被收藏(${lovenum || 0})</span>
                       <span class="text-muted post-views"> <i class="fa thumbs-up"></i> ${tags || ''}</span>
                    </a>
                  </li>
      `
    }
  }).join('')
  $('.items-01').html(htmlStr || '暂无文章数据。。。')
  
  if (!count) {
    $('#page-box').hide()
  }
    renderPage(count)
  
}
myArticle()

// 修改文章
$('.items-01').on('click', '.btn-success', async function (e) {
  const id = $(this).attr('data-id')
  const res = await axios.post('/user/editArticle', artinfo[id])
  // ！此处未考虑失败情况
  layer.msg('发布成功!', {
    icon: 6,
    offset: '20px'
  })
  myArticle()
})

// 删除文章
$('.items-01').on('click', '.btn-danger', async function (e) {
  const id = $(this).attr('data-id')
  // console.log(id)
  const res = await axios.get(`/user/delArticle?id=${id}`)
  // ！此处未考虑失败情况
  layer.msg('删除成功!', {
    icon: 6,
    offset: '20px'
  })
  myArticle()
})


// 修改文章
$('.items-01').on('click', '.btn-primary', async function (e) {
  const id = $(this).attr('data-id')
  // 跳转时用查询字符串方式带上当前需要修改的文章的Id
  location.href = './user-editart.html?id=' + id
  console.log(id)
})


const laypage = layui.laypage
function renderPage(count) {
  laypage.render({
    elem: 'page-box'
    , count: count
    , theme: '#FF5722'
    , limit: '3'
    , curr: query.page
    , last: '尾页'
    , layout: ['page', 'next', 'count']
    , jump: function (obj, first) {
      //obj包含了当前分页的所有参数，比如：
      // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
      // console.log(obj.limit); //得到每页显示的条数
      query.page = obj.curr
      query.limit = obj.limit
      //首次不执行
      if (!first) {
        myArticle()
      }
    }
  })
}
